<template>
  <div class="add-task">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>添加新任务</span>
      </div>
      <el-form :model="taskForm" @submit.native.prevent="addTask" label-width="100px">
        <el-form-item label="任务名称">
          <el-input v-model="taskForm.name" placeholder="请输入任务名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addTask">添加任务</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      taskForm: {
        name: '',
        completed: false // 默认新任务为未完成状态
      }
    };
  },
  methods: {
    addTask() {
      const token = localStorage.getItem('token');
      axios.post('/api/tasks/add', this.taskForm, {
        headers: {
          'Authorization': token
        }
      })
          .then(() => {
            this.$message.success('任务已添加');
            this.taskForm.name = ''; // 清空表单
            this.$router.push('/todolist'); // 假设你有一个路由用于添加任务
          })
          .catch(error => {
            this.$message.error('添加任务失败');
            console.error('Error:', error);
          });
    }
  }
};
</script>

<style scoped>
.add-task {
  margin: 20px;
}
.box-card {
  width: 300px;
}
</style>
